package net.oschina.allchat.protocol.core.stanza;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.oschina.allchat.protocol.core.ChatId;
import net.oschina.allchat.protocol.core.Protocol;

public class Stanza {
	
	protected ChatId from;
	protected ChatId to;
	protected String id;
	protected String lang;
	protected List<Object> objects;
	protected Map<Class<?>, Protocol> objectProtocols;
	protected String originalMessage;
	
	public Stanza() {
	}
	
	public ChatId getFrom() {
		return from;
	}

	public String getId() {
		return id;
	}

	public ChatId getTo() {
		return to;
	}

	public String getLang() {
		return lang;
	}

	public void setFrom(ChatId from) {
		this.from = from;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setTo(ChatId to) {
		this.to = to;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public void setObjects(List<Object> objects) {
		this.objects = objects;
	}
	
	public List<Object> getObjects() {
		if (objects == null) {
			objects = new ArrayList<Object>(0);
		}
		
		return objects;
	}
	
	public void setObject(Object object) {
		if (objects != null) {
			objects.clear();
		} else {
			objects = new ArrayList<Object>(1);
		}
		
		objects.add(object);
	}
	
	public Object getObject() {
		if (objects == null || objects.size() == 0)
			return null;
		
		return objects.get(0);
	}
	
	public void addObjectProtocol(Class<?> clazz, Protocol protocol) {
		if (objectProtocols == null) {
			objectProtocols = new HashMap<Class<?>, Protocol>();
		}
		
		objectProtocols.put(clazz, protocol);
	}
	
	public void setObjectProtocols(Map<Class<?>, Protocol> protocols) {
		this.objectProtocols = protocols;
	}
	
	public Map<Class<?>, Protocol> getObjectProtocols() {
		return objectProtocols;
	}
	
	public Protocol getObjectProtocol(Object object) {
		if (objectProtocols == null)
			return null;
		
		return objectProtocols.get(object.getClass());
	}
	
	public Protocol getObjectProtocol() {
		return getObjectProtocol(getObject());
	}
	
	public void setOriginalMessage(String originalMessage) {
		this.originalMessage = originalMessage;
	}
	
	public String getOriginalMessage() {
		return originalMessage;
	}

}
